//import 'package:amap_base/amap_base.dart';

import 'dart:async';
import 'dart:convert';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_hotwork/bean/UserTaskDetailBean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';

import '../Toast.dart';

class TaskAmountActivity extends StatefulWidget {
  @override
  taskState createState() => new taskState();
}

class taskState extends State<TaskAmountActivity> {

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
    queryData();
  }
  Map selectArea;
  final textEditingController = TextEditingController();
  List<Result> searchbean = new List();
  var time = formatDate(new DateTime.now(),[yyyy,'-',mm,"-",dd]);
  var time2 = formatDate(new DateTime.now(),[yyyy,'-',mm,"-",dd]);
  ///接收选择的结果
  void handleSelect (Map targetArea) {
    setState(() {
      selectArea = targetArea;
      // initData(selectArea['proviceIndex'],selectArea['cityIndex']);
    });
  }
//手机号的控制器
  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }
  ScrollController controller = ScrollController();
  UserTaskDetailBean userTaskDetailBean;
  void queryData() async {
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX+"/checkTask/statisticalAnalysis?endDate="+(time2+" 0:0:0")+"&&startDate="+(time+" 23:59:59"));
      Map respMap = json.decode(response.data);
      userTaskDetailBean  = UserTaskDetailBean.fromJson(respMap);
      print('响应数据：' + response.toString());
      setState(() {

      });
    } catch (e) {
      //  LocalToast.toast(context,msg: "网络错误");
      print('请求出错：' + e.toString());
    }

  }

  genUserList(context, controller) {

    if(searchbean.length>0&&textEditingController.text.length>0){
      return  genContentList(
          searchbean, context, controller
      );
    }else{
      if(null!=userTaskDetailBean&&null!=userTaskDetailBean.result){
        return  genContentList(
            userTaskDetailBean.result, context, controller
        );
      }else{
        return Container();
      }

    }
  }
  @override
  Widget build(BuildContext context) {
    double rpx = MediaQuery
        .of(context)
        .size
        .height / 800;
    double rpxw = MediaQuery
        .of(context)
        .size
        .width / 750;

    textEditingController.addListener(() {
      print('input:${textEditingController.text}');

      searchbean.clear();
      for(int i = 0;i<userTaskDetailBean.result.length;i++){
        if(userTaskDetailBean.result[i].realName.contains(textEditingController.text)){
          searchbean.add(userTaskDetailBean.result[i]);
        }
      }
      setState(() {

      });
    });

    return Scaffold(
      appBar:new BaseViewBar(
          childView:
          Column(
            children: [
              new BaseTitleBar(
                "巡检任务统计",
                leftIcon: 'images/fanhui.png',
                rightIcon:  "",
                rightClick: () {
                  print("点击了右边干嘛啊。。。哦");
                },
                leftClick: () {
                  Navigator.of(context).pop();
                },
              )
              ,
            ],
          ),
          preferredSize: Size.fromHeight(58.0)

      ),
      body:
      Container(
          padding: EdgeInsets.only(left: 10),
          child:
              userTaskDetailBean!=null?
                Column(
                  children: [
                    Container(
                      height: 30,
                      margin: EdgeInsets.only(left: 0, top: 10, right: 10, bottom: 10),
                      width: rpx * 600,
                      padding: EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                      decoration: BoxDecoration(
                        border: new Border.all(color: Color(0xFF9E9E9E), width: 0.5),
                        // 边色与边宽度
                        color: Colors.white,
                        // 底色
                        //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                        borderRadius: BorderRadius.all(Radius.circular(15)),
                      ),
                      child:
                          Center(child: TextField(
                            controller: textEditingController,
                            style: TextStyle(color: Colors.grey),
//                            controller: phoneController,
                            decoration: InputDecoration(
                              icon: Icon(Icons.search),
                              contentPadding: EdgeInsets.all(8.0),
                              hintText: '搜索用户名',
                              hintStyle: TextStyle(color: Colors.grey),
//                            helperText: '请输入注册的手机号',
                              border: InputBorder.none,
                            ),
                            autofocus: false,
                          ),),
                    ),
                    Row(
                      children: <Widget>[
                        Text("开始时间："),
                        FlatButton(
                          onPressed: () {
                            DatePicker.showDatePicker(context,
                                // 是否展示顶部操作按钮
                                showTitleActions: true,
                                // 最小时间
                                minTime: DateTime(2018, 3, 5),
                                // 最大时间
                                maxTime: DateTime(2099, 6, 7),
                                // change事件
                                onChanged: (date) {


                                  print('change $date');
                                },
                                // 确定事件
                                onConfirm: (date) {
                                  time = formatDate(date,[yyyy,'-',mm,"-",dd]);
                                  setState(() {
                                    queryData();
                                  });
                                  print('confirm $time');
                                },
                                // 当前时间
                                currentTime: DateTime.now(),
                                // 语言
                                locale: LocaleType.zh);
                          },
                          // onPressed: () {
                          //   DatePicker.showDateTimePicker(context,
                          //       // 是否展示顶部操作按钮
                          //       showTitleActions: true,
                          //       // 最小时间
                          //       minTime: DateTime(2018, 3, 5),
                          //       // 最大时间
                          //       maxTime: DateTime(2099, 6, 7),
                          //       // change事件
                          //       onChanged: (date) {
                          //
                          //
                          //         print('change $date');
                          //       },
                          //       // 确定事件
                          //       onConfirm: (date) {
                          //         time = formatDate(date,[yyyy,'-',mm,"-",dd,"  ",HH,":",nn]);
                          //         setState(() {
                          //
                          //         });
                          //         print('confirm $time');
                          //       },
                          //       // 当前时间
                          //       currentTime: DateTime.now(),
                          //       // 语言
                          //       locale: LocaleType.zh);
                          // },
                          child:
                          Container(
                              width: 100,
                              height: 20,
                              margin: EdgeInsets.only(left: 0, right: 0, top:0, bottom: 0),
                              padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                              decoration: BoxDecoration(
                                border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                color: Colors.white, // 底色
                                //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                              ),
                              child:
                              Row(
                                children: [
                                  Image.asset(
                                    'images/rili.png',
                                    width: 14.0,
                                    height: 20.0,
                                  ),
                                  Container(width: 5,),
                                  Text(
                                    '$time',
                                    style: TextStyle(color: Colors.black,fontSize: 10),
                                  )

                                ],
                              )
                          ),
                        ),
                      ],
                    ),
                    Row(
                      children: <Widget>[
                        Text("结束时间："),
                        FlatButton(
                          onPressed: () {
                            DatePicker.showDatePicker(context,
                                // 是否展示顶部操作按钮
                                showTitleActions: true,
                                // 最小时间
                                minTime: DateTime(2018, 3, 5),
                                // 最大时间
                                maxTime: DateTime(2099, 6, 7),
                                // change事件
                                onChanged: (date) {


                                  print('change $date');
                                },
                                // 确定事件
                                onConfirm: (date) {
                                  time2 = formatDate(date,[yyyy,'-',mm,"-",dd]);
                                  setState(() {
                                    queryData();
                                  });
                                  print('confirm $time2');
                                },
                                // 当前时间
                                currentTime: DateTime.now(),
                                // 语言
                                locale: LocaleType.zh);
                          },
                          child:
                          Container(
                              width: 100,
                              height: 20,
                              margin: EdgeInsets.only(left: 0, right: 0, top:0, bottom: 0),
                              padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                              decoration: BoxDecoration(
                                border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                color: Colors.white, // 底色
                                //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                              ),
                              child:
                              Row(
                                children: [
                                  Image.asset(
                                    'images/rili.png',
                                    width: 14.0,
                                    height: 20.0,
                                  ),
                                  Container(width: 5,),
                                  Text(
                                    '$time2',
                                    style: TextStyle(color: Colors.black,fontSize: 10),
                                  )

                                ],
                              )
                          ),
                        ),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: MainAxisSize.max,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                      crossAxisAlignment: CrossAxisAlignment.center,
                      //就是字child的垂直布局方向，向上还是向下
                      verticalDirection: VerticalDirection.down,
                      children: [
                        Container(
                            height: 30,
                            width: rpxw*150,
                            child:
                            Center(
                              child:
                              Text("用户名",
                                style: TextStyle(color: Colors.black, fontSize: 12,),
                              ),
                            )
                        ),
                        Container(
                            height: 30,
                            width: rpxw*150,
                            child:
                            Center(
                              child:
                              Text("巡检次数",
                                style: TextStyle(color: Colors.black, fontSize: 12,),
                              ),
                            )
                        ),
                        Container(
                            height: 30,
                            width: rpxw*150,
                            child:
                            Center(
                              child:
                              Text("巡检里程",
                                style: TextStyle(color: Colors.black, fontSize: 12,),
                              ),
                            )
                        ),
                        Container(
                            height: 30,
                            width: rpxw*150,
                            child:
                            Center(
                              child:
                              Text("问题数量",
                                style: TextStyle(color: Colors.black, fontSize: 12,),
                              ),
                            )
                        ),

                      ],

                    ),
                    Expanded(
                      child:
                      genUserList(
                         context, controller
                      ),
                    ),

                  ],
                )
                  :
              Container(),



      ),
    );
  }
  genContentList(List<Result> data, BuildContext context, controller) {
    double rpx = MediaQuery
        .of(context)
        .size
        .width / 750;

    return ListView.builder(
      shrinkWrap: true,
      controller: controller,
      itemCount: data.length,
      itemBuilder: (BuildContext context, int index) {
        return
          Card(
            // padding: EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
            child:
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
              crossAxisAlignment: CrossAxisAlignment.center,
              //就是字child的垂直布局方向，向上还是向下
              verticalDirection: VerticalDirection.down,
              children: [
                Container(
                    height: 30,
                    width: rpx*150,
                    child:
                    Center(
                      child:
                      Text(data[index].realName,
                        style: TextStyle(color: Colors.black, fontSize: 12,),
                      ),
                    )
                ),
                Container(
                    height: 30,
                    width: rpx*150,
                    child:
                    Center(
                      child:
                      Text(data[index].checkNumber.toString(),
                        style: TextStyle(color: Colors.black, fontSize: 12,),
                      ),
                    )
                ),
                Container(
                    height: 30,
                    width: rpx*150,
                    child:
                    Center(
                      child:
                      Text(data[index].checkMileage.toString(),
                        style: TextStyle(color: Colors.black, fontSize: 12,),
                      ),
                    )
                ),
                Container(
                    height: 30,
                    width: rpx*150,
                    child:
                    Center(
                      child:
                      Text(data[index].checkQuestionNumber.toString(),
                        style: TextStyle(color: Colors.black, fontSize: 12,),
                      ),
                    )
                ),

              ],

            ),

          );
        // Expanded(
        //   child:
        // Flexible(
        //   child:
        // getItemContentList(data[index].focusHotUserVos,context,controller),
        // )
        // )

      },
    );
  }


}






















